package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.annotation.ValidateAnnotation;
import com.csun.dsms.comment.core.support.CsunMessageSource;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsMenu;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.dto.menu.*;
import com.csun.dsms.model.vo.menu.MenuVo;
import com.csun.dsms.model.vo.menu.ViewMenuVo;
import com.csun.dsms.service.DsmsMenuService;
import com.csun.dsms.validator.DsmsMenuAddValidator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-01 11:52
 * @description: 菜单管理拦截器
 */
@RestController
@RequestMapping("/menu")
public class DsmsMenuController {

    @Resource
    private DsmsMenuService dsmsMenuService;
    @Resource
    private CsunMessageSource csunMessageSource;

    /**
     * 检测菜单编码是否已存在
     *
     * @param menuCheckCodeDto  the menu check code dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkMenuCode")
    public Wrapper<Boolean> checkUacMenuCode(@RequestBody MenuCheckCodeDto menuCheckCodeDto) {

        boolean stat = dsmsMenuService.checkMenuCode(menuCheckCodeDto);

        return WrapMapper.ok(stat);
    }

    /**
     * 检测菜单名称唯一性
     *
     * @param menuCheckNameDto the menu check name dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkMenuName")
    public Wrapper<Boolean> checkUacMenuName(@RequestBody MenuCheckNameDto menuCheckNameDto) {

        boolean stat = dsmsMenuService.checkMenuName(menuCheckNameDto);

        return WrapMapper.ok(stat);
    }


    /**
     * 检测菜单URL唯一性
     *
     * @param menuCheckUrlDto the menu check url dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/checkMenuUrl")
    public Wrapper<Boolean> checkUacMenuUrl(@RequestBody MenuCheckUrlDto menuCheckUrlDto) {

        boolean stat = dsmsMenuService.checkMenuUrl(menuCheckUrlDto);

        return WrapMapper.ok(stat);
    }

    /**
     * 获取菜单列表数据
     *
     * @return the wrapper
     */
    @Authorization
    @PostMapping(value = "/getTree")
    public Wrapper<List<MenuVo>> queryMenuTreeList() {

        List<MenuVo> menuVoList = dsmsMenuService.getMenuVoList();

        return WrapMapper.ok(menuVoList);
    }


    /**
     * 查询菜单详情
     *
     * @param id the id
     *
     * @return the wrapper
     */
    @PostMapping(value = "/queryById/{id}")
    public Wrapper queryMenuVoById(@PathVariable Long id) {

        ViewMenuVo menuVo = dsmsMenuService.getViewVoById(id);

        return WrapMapper.ok(menuVo);
    }


    /**
     * 查询菜单及下面所有子菜单
     *
     * @param menuQueryDto	the menu query dto
     *
     * @return	the wrapper
     */
    @PostMapping(value = "/queryMenuAndSubMenuListWithPage")
    public Wrapper queryMenuAndSubMenuListWithPage(@RequestBody MenuQueryDto menuQueryDto) {

        PageHelper.startPage(menuQueryDto.getPageNum(), menuQueryDto.getPageSize());

        List<DsmsMenu> menuList = dsmsMenuService.queryMenuAndSubMenuListWithPage(menuQueryDto);

        return WrapMapper.ok(new PageInfo<>(menuList));
    }

    /**
     * 根据id修改菜单的禁用状态
     *
     * @param menuStatusDto the uac menu status dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/modifyStatus")
    public Wrapper updateUacMenuStatusById(@CurrentUser DsmsUser login, @RequestBody MenuStatusDto menuStatusDto) {

        Integer result = dsmsMenuService.updateMenuStatusById(menuStatusDto, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 新增菜单.
     *
     * @param menuAddDto the menu add dto
     *
     * @return the wrapper
     */
    @ValidateAnnotation
    @PostMapping(value = "/save")
    public Wrapper saveMenu(@CurrentUser DsmsUser login,
                            @Validated(DsmsMenuAddValidator.class) @RequestBody EditMenuDto menuAddDto,
                            BindingResult br) {

        DsmsMenu dsmsMenu = new DsmsMenu();
        BeanUtils.copyProperties(menuAddDto, dsmsMenu);
        Integer result = dsmsMenuService.save(dsmsMenu, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 修改菜单信息
     *
     * @param dsmsMenu	the dsms menu
     *
     * @return	the wrapper
     */
    @PostMapping(value = "/edit")
    public Wrapper editUacMenu(@CurrentUser DsmsUser login, @RequestBody DsmsMenu dsmsMenu) {

        Integer result = dsmsMenuService.editMenu(dsmsMenu, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 根据id删除菜单
     *
     * @param id the id
     *
     * @return the wrapper
     */
    @PostMapping(value = "/deleteById/{id}")
    public Wrapper<Integer> deleteUacMenuById(@CurrentUser DsmsUser login, @PathVariable Long id) {

        Preconditions.checkArgument(id != null, "菜单ID不能为空");

        // 判断此菜单是否有子节点
        boolean hasChild = dsmsMenuService.checkMenuHasChildMenu(id);
        if (hasChild) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "此菜单含有子菜单, 请先删除子菜单");
        }

        // 判断菜单是有功能
//        UacAction uacAction = new UacAction();
//        uacAction.setMenuId(id);
//        int count = uacActionService.selectCount(uacAction);
//        if (count > 0) {
//            throw new UacBizException(ErrorCodeEnum.UAC10013019);
//        }

        int result = dsmsMenuService.deleteMenuById(id, login);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, csunMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }
}
